#include <iostream>
#include <vector>

using namespace std;
class MyCalendarTwo
{
public:
    MyCalendarTwo()
    {
    }

    bool book(int start, int end)
    {
        for (auto &[l, r] : overlaps)
        {
            if (l < end && start < r)
            {
                return false;
            }
        }
        for (auto &[l, r] : booked)
        {
            if (l < end && start < r)
            {
                overlaps.emplace_back(max(l, start), min(r, end));
            }
        }
        booked.emplace_back(start, end);
        return true;
    }

private:
    vector<pair<int, int>> booked;
    vector<pair<int, int>> overlaps;
};
class MyCalendarTwo {
public:
    MyCalendarTwo() {

    }

    bool book(int start, int end) {
        int ans = 0;
        int maxBook = 0;
        cnt[start]++;
        cnt[end]--;
        for (auto &[_, freq] : cnt) {
            maxBook += freq;
            ans = max(maxBook, ans);
            if (maxBook > 2) {
                cnt[start]--;
                cnt[end]++;
                return false;
            }
        }
        return true;
    }
private:
    map<int, int> cnt;
};


int main()
{
    MyCalendarTwo myCalendar = MyCalendarTwo();
    myCalendar.book(10, 20); // returns true
    myCalendar.book(50, 60); // returns true
    myCalendar.book(10, 40); // returns true
    myCalendar.book(5, 15);  // returns false
    myCalendar.book(5, 10);  // returns true
    myCalendar.book(25, 55); // returns true

    system("pause");
    return 0;
}
